package com.bsoft.gol.hcs.clinical.dao;

import com.bsoft.gol.hcs.clinical.bean.response.QueryMedicalSkillTotalResp;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.annotation.RpcService;
import ctd.util.converter.ConversionUtils;
import hcn.base.CheckRequisitionRecord;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;

import java.util.Date;
import java.util.List;
import java.util.Map;

public abstract class CheckRequisitionRecordDAO extends HibernateSupportDelegateDAO<CheckRequisitionRecord> {

    /**
     * 获取检验列表
     *
     * @param consultId
     * @return
     */
    @RpcService
    @DAOMethod(sql = " from CheckRequisitionRecord where consultId =:consultId and applyStatus=:applyStatus ")
    public abstract List<CheckRequisitionRecord> queryCheckRequisitionRecord(@DAOParam("consultId") String consultId
            , @DAOParam("applyStatus") String applyStatus);
    @RpcService
    @DAOMethod(sql = " from CheckRequisitionRecord where consultId =:consultId  ")
    public abstract List<CheckRequisitionRecord> queryCheckRequisitionRecordByConsultId(@DAOParam("consultId") String consultId);
    @RpcService
    @DAOMethod(sql = "delete from CheckRequisitionRecord where checkRequisitionRecordId=:checkRequisitionRecordId")
    public abstract void deleteCheckRequisitionRecord(@DAOParam(value = "checkRequisitionRecordId") String checkRequisitionRecordId);
    @RpcService
    @DAOMethod(sql = "update CheckRequisitionRecord set applyStatus='1', applicationNo =:applicationNo " +
            ",submissionTime=now() where checkRequisitionRecordId =:checkRequisitionRecordId")
    public abstract void updateCheckRequisitionRecordState(@DAOParam("checkRequisitionRecordId") String checkRequisitionRecordId,
                                                           @DAOParam("applicationNo") String applicationNo);
    @RpcService
    @DAOMethod(sql = "update CheckRequisitionRecord set applyStatus='0' where checkRequisitionRecordId=:checkRequisitionRecordId")
    public abstract void updateCheckRequisitionRecordReturnState(@DAOParam("checkRequisitionRecordId") String checkRequisitionRecordId);

    @RpcService
    @DAOMethod(sql = " from CheckRequisitionRecord where consultId =:consultId ")
    public abstract CheckRequisitionRecord getCheckRequisitionRecord(@DAOParam("consultId") String consultId);
    @RpcService
    @DAOMethod(sql = " from CheckRequisitionRecord where checkRequisitionRecordId =:checkRequisitionRecordId ")
    public abstract CheckRequisitionRecord getCheckRequisitionRecordByKey(@DAOParam("checkRequisitionRecordId") String checkRequisitionRecordId);
    @RpcService
    @DAOMethod(sql = " from CheckRequisitionRecord where applicationNo =:applicationNo ")
    public abstract List<CheckRequisitionRecord> queryCheckRequisitionRecordByApplicationNo(@DAOParam("applicationNo") String applicationNo);
    /**
     * 获取不同状态总数
     *
     * @param consultId
     * @return
     */
    @RpcService
    public QueryMedicalSkillTotalResp queryCheckRequisitionTotal(final String consultId) {
        HibernateStatelessResultAction<QueryMedicalSkillTotalResp> action = new AbstractHibernateStatelessResultAction<QueryMedicalSkillTotalResp>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer sql = new StringBuffer();
                sql.append("select SUM(CASE WHEN apply_status=0 THEN 1 ELSE 0 END) as uncommittedNum,");
                sql.append("SUM(CASE WHEN apply_status=1 THEN 1 ELSE 0 END) as committedNum,");
                sql.append("SUM(CASE WHEN apply_status=2 THEN 1 ELSE 0 END) as reservedNum,");
                sql.append("SUM(CASE WHEN apply_status=3 THEN 1 WHEN apply_status=4 THEN 1 ELSE 0 END) as executedNum FROM check_requisition_record ");
                sql.append("where consult_id=:consultId ");
                Query query = ss.createSQLQuery(sql.toString());
                query.setParameter("consultId", consultId);
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> list = query.list();
                if (list.size() == 1) {
                    Object o = list.get(0);
                    QueryMedicalSkillTotalResp queryMedicalSkillTotalResp = ConversionUtils.convert(o, QueryMedicalSkillTotalResp.class);
                    setResult(queryMedicalSkillTotalResp);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
    @RpcService
    @DAOMethod(sql = " update CheckRequisitionRecord set reportTime=:reportDate,reportDoctorId=:reportDoctorId," +
            "reportDoctorName=:reportDoctorName,reportUrl=:reportUrl,pictureUrl=:pictureUrl,studyId=:studyID" +
            " where checkRequisitionRecordId=:id")
    public abstract void updateCheckReportForm(@DAOParam("id") String checkRequisitionRecordId, @DAOParam("reportDate") Date reportDate, @DAOParam("reportDoctorId") String reportDoctorId,
                                               @DAOParam("reportDoctorName") String reportDoctorName, @DAOParam("pictureUrl") String pictureUrl,
                                               @DAOParam("reportUrl") String reportUrl, @DAOParam("studyID") String studyID);
    @RpcService
    @DAOMethod(sql = " update CheckRequisitionRecord set applyStatus=:applyStatus where applicationNo=:applicationNo")
    public abstract void updateApplyStatus(@DAOParam("applicationNo") String applicationNo, @DAOParam("applyStatus") String applyStatus);
    @RpcService
    @DAOMethod(sql = " update CheckRequisitionRecord set chargeStatus=:chargeStatus where applicationNo=:applicationNo")
    public abstract void updateChargeStatus(@DAOParam("applicationNo") String applicationNo, @DAOParam("chargeStatus") String chargeStatus);

    @RpcService
    @DAOMethod(sql = " update CheckRequisitionRecord set applyStatus=:applyStatus, applicationNo = null where applicationNo=:applicationNo")
    public abstract void updateApplyStatusNull(@DAOParam("applicationNo") String applicationNo, @DAOParam("applyStatus") String applyStatus);

    @RpcService
    @DAOMethod(sql = " update CheckRequisitionRecord set payStatus=:payStatus,chargeStatus=:payStatus where applicationNo in:applicationNoList")
    public abstract void updatePayStatus(@DAOParam("applicationNoList") List<String> applicationNoList, @DAOParam("payStatus") String payStatus);

    /**
     * @description: 修改申请单为已报告
     * @author: chenJij@bsoft.com.cn
     */
    @RpcService
    @DAOMethod(sql = " update CheckRequisitionRecord set applyStatus='4' where  organizationId=:organizationId and applicationNo=:applicationNo")
    public abstract void updateReportStatus(@DAOParam("organizationId") String organizationId, @DAOParam("applicationNo") String applicationNo);
}
